package cn.lisens.modules.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/")
public class LoginController {
	
	/**
	 * 登录页面
	 */
	@GetMapping(value = "login")
	public String login(HttpServletRequest request, HttpServletResponse response, Model model, String login_type) {
		return "modules/sys/sysLogin";
	}
	
	
	@PostMapping(value = "/login")
//	@ResponseBody
    public String login(String username,String password, HttpServletResponse response) {
        Subject user = SecurityUtils.getSubject();
        System.out.println(username +" - "+password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            user.login(token);
            return "redirect:index";
        } catch (AuthenticationException errro) {
            return "modules/sys/sysLogin";
        }

        
    }
	
	@RequestMapping("/logout2")
	public ModelAndView user(HttpServletRequest request, HttpServletResponse response) {
		SecurityUtils.getSubject().logout();
        return new ModelAndView("redirect:login");
	}
	
	/**
	 * 登录成功，进入管理首页
	 */
	@RequestMapping(value = "index")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/sys/sysIndex";
	}
}
